// jqgrid表格配置
// 初始化jqGrid
// 状态显示
function subTaskState(subTaskState) {
    if (subTaskState == 0) {
        return '未处理';
    } else if (subTaskState == 1) {
        return '未审核';
    } else if (subTaskState == 2) {
        return '已完成';
    } else if (subTaskState == 3) {
        return '待整改';
    }
}

// 操作按钮
function check() {
    var check = "";
    check = "<a href='#qualitycontrol-assignmentGroup-checkProcessed' class='btn btn-primary btn-sm check' title='查看'>查看</a>";
    return check;
};

function pageInit() {
    var parentHeight, parentWidth;
    parentHeight = $("#processed-table").height() - 23,
        parentWidth = $("#processed-table").width() + 14;
    //创建jqGrid组件
    jQuery("#processedList").jqGrid({
        url: contextPath + "/subtask/queryHistoryTodoSubTaskJqg",
        datatype: "json",
        height: parentHeight,
        width: parentWidth,
        colNames: ['子任务Id', '任务名称', '设备编号', '设备类型Id', '设备类型', '工单类型Id', '工单类型', '施工单位', '项目名称', '产品线', '状态', '派单时间', '完成时间', '备注信息', '操作'],
        colModel: [
            {name: 'subTaskId', index: 'subTaskId', sortable: false, hidden: true},
            {name: 'subTaskName', index: 'subTaskName', sortable: false},
            {name: 'deviceId', index: 'deviceId', sortable: false},
            {name: 'deviceTypeId', index: 'deviceTypeId', sortable: false, hidden: true},
            {name: 'deviceTypeName', index: 'deviceTypeName', sortable: false},
            {name: 'workOrderTypeId', index: 'workOrderTypeId', sortable: false, hidden: true},
            {name: 'workOrderTypeName', index: 'workOrderTypeName', sortable: false},
            {name: 'organizationTeamName', index: 'organizationTeamName', sortable: false},
            {name: 'projectName', index: 'projectName', sortable: false},
            {name: 'productLineName', index: 'productLineName', sortable: false},
            {name: 'subTaskState', index: 'subTaskState', sortable: false, formatter: subTaskState},
            {name: 'assignTaskTime', index: 'assignTaskTime', sortable: false},
            {name: 'endTaskTime', index: 'endTaskTime', sortable: false},
            {name: 'description', index: 'description', sortable: false},
            {name: 'subTaskState', index: 'subTaskState', sortable: false, formatter: check}
        ],
        sortname: '',
        sortorder: 'desc',
        viewrecords: true,
        rowNum: 20,
        rowList: [20, 50, 100],
        pager: "#gridpager"
    });
    /*创建jqGrid的操作按钮容器*/
    jQuery("#processedList").jqGrid('navGrid', '#gridpager', {edit: false, add: false, del: false, search: false});
};
//函数：获取尺寸　
var timer = 0;
$(window).resize(function () {
    clearTimeout(timer);
    timer = setTimeout(function () {
        $("#processedList").setGridWidth($('#processed-table').width() + 12);
        $("#processedList").setGridHeight($('#processed-table').height() - 23);
    }, 260);
});
// 侧边栏缩小时，实时的给jqgrid的宽和高赋值
$("#asidebarSwitch").on('click', function () {
    var time = 0;
    var set = setInterval(function () {
        $("#processedList").setGridWidth($('#processed-table').width() * 1 + 14);
        $("#processedList").setGridHeight($('#processed-table').height() * 1 - 23);
        time++;
        if (time == 201) {
            clearInterval(set);
        }
    }, time);
});

// 查询区域
// 查询所有产品线
$.ajax({
    url: contextPath + "/ProductLine/queryAllProductLine",
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        var str = '<option value="" selected>选择产品线</option>';
        for (var i = 0, len = data.length; i < len; i++) {
            str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#productLineId").html(str);
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});
// 查询设备类型
$.ajax({
    url: contextPath + "/DeviceType/queryAllDeviceType",
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        var str = '<option value="" selected>选择设备类型</option>';
        for (var i = 0, len = data.length; i < len; i++) {
            str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#deviceTypeId").html(str);
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});
// 查询工单类型
$.ajax({
    url: contextPath + "/WorkOrderType/queryAllWorkOrderType",
    type: 'GET',
    dataType: "json",
    contentType: "application/json",
    success: function (data) {
        var str = '<option value="" selected>选择工单类型</option>';
        for (var i = 0, len = data.length; i < len; i++) {
            str += '<option value="' + data[i].id + '">' + data[i].name + "</option>";
        }
        $("#workOrderTypeId").html(str);
    },
    error: function (data) {
        toastr.error(data.message, "请求失败");
    }
});

// 查询
$('#query').on('click', function () {
    var productLineId = $("#productLineId").val();
    var projectName = $("#projectName").val();
    var constructionTeamName = $("#constructionTeamName").val();
    var deviceName = $("#deviceName").val();
    var deviceTypeId = $("#deviceTypeId").val();
    var workOrderTypeId = $("#workOrderTypeId").val();
    var subTaskState = $("#subTaskState").val();
    $("#processedList").setGridParam({
        url: contextPath + "/subtask/queryHistoryTodoSubTaskJqg",
        postData: {
            queryCond: {
                productLineId: productLineId,
                projectName: projectName,
                constructionTeamName: constructionTeamName,
                deviceName: deviceName,
                deviceTypeId: deviceTypeId,
                workOrderTypeId: workOrderTypeId,
                subTaskState: subTaskState
            }
        },
        page: 1
    }).trigger("reloadGrid");
});
// 查看
// 获取子任务数据
$("#processedList").on('click', '.check', function () {
    var id = $(this).parent().parent().attr('id');
    var subTaskCheckData = $("#processedList").jqGrid('getRowData', id);
    console.log(subTaskCheckData.subTaskId);
    $.cookie('subTaskCheckId', JSON.stringify(subTaskCheckData.subTaskId));
});